﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace Argos.Windows.Forms
{
	public partial class TypeSelectorDialog: Form
	{
		public TypeSelectorDialog()
		{
			InitializeComponent();
			this.Size = Settings.Default.FormSize;
		}

		public TypeSelectorDialog(Predicate<Type> typeMatcher) : this()
		{
			if (null == typeMatcher)
			{
				throw new ArgumentNullException("typeMatcher");
			}

			this.Selector.TypeMatcher = typeMatcher;
		}


		/// <summary>
		/// Sets or returns whether to allow users to select more than one type in the selector.
		/// </summary>
		public bool MultiSelect
		{
			get { return this.Selector.MultiSelect; }
			set { this.Selector.MultiSelect = value; }
		}

		/// <summary>
		/// Returns the types selected in the selector if the user clicked the <c>OK</c>
		/// button in the dialog.
		/// </summary>
		public IList<Type> SelectedTypes
		{
			get { return this.Selector.SelectedTypes; }
		}

		public Predicate<Type> TypeMatcher
		{
			get { return this.Selector.TypeMatcher; }
			set { this.Selector.TypeMatcher = value; }
		}


		protected override void OnClosing(CancelEventArgs e)
		{
			base.OnClosing(e);

			if (!e.Cancel && this.WindowState == FormWindowState.Normal)
			{
				Settings.Default.FormSize = this.Size;
				Settings.Default.Save();
			}

		}



		private void Selector_TypesSelected(object sender, EventArgs e)
		{
			this.OK.PerformClick();
		}


		private class Settings : ApplicationSettingsBase
		{

			public static Settings Default = (Settings)ApplicationSettingsBase.Synchronized(new Settings());

			[UserScopedSetting]
			[DefaultSettingValue("400, 300")]
			public Size FormSize
			{
				get { return (Size)this["FormSize"]; }
				set { this["FormSize"] = value; }
			}

		}

	}
}
